package com.ruoyi.fun.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysUserService;

/**
 * 移动端用户Controller（无需权限验证）
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@Anonymous
@RestController
@RequestMapping("/mobile/user")
public class EcMobileUserController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    /**
     * 根据用户ID获取用户头像信息
     */
    @GetMapping("/avatar/{userId}")
    public AjaxResult getUserAvatar(@PathVariable("userId") Long userId)
    {
        try {
            SysUser user = userService.selectUserById(userId);
            if (user == null) {
                return AjaxResult.error("用户不存在");
            }
            
            // 返回用户头像信息
            return AjaxResult.success("查询成功", user.getAvatar());
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID获取用户基本信息（包含头像）
     */
    @GetMapping("/info/{userId}")
    public AjaxResult getUserInfo(@PathVariable("userId") Long userId)
    {
        try {
            SysUser user = userService.selectUserById(userId);
            if (user == null) {
                return AjaxResult.error("用户不存在");
            }
            
            // 只返回必要的用户信息
            SysUser userInfo = new SysUser();
            userInfo.setUserId(user.getUserId());
            userInfo.setUserName(user.getUserName());
            userInfo.setNickName(user.getNickName());
            userInfo.setAvatar(user.getAvatar());
            
            return AjaxResult.success("查询成功", userInfo);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
    /**
     * 根据id和组织ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ecmobileuser:query')")
    @GetMapping(value = "/{id}/org/{orgId}")
    public AjaxResult getInfoByOrgId(@PathVariable("id") Long id, @PathVariable("orgId") Long orgId)
    {
        SysUser user = userService.selectUserById(id);
        if (user == null) {
            return AjaxResult.error("用户不存在或已被删除");
        }
        return success(user);
    }

    /**
     * 根据组织ID查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:ecmobileuser:list')")
    @GetMapping("/org/{orgId}")
    public TableDataInfo listByOrgId(@PathVariable Long orgId)
    {
        startPage();
        SysUser sysUser = new SysUser();
        List<SysUser> list = userService.selectUserList(sysUser);
        return getDataTable(list);
    }


}